/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package businesscard.DAO;

import bc.entity.Baiviet;
import bc.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author THICH HOA DONG TIEN
 */
public class BaivietDAO {
     private Session session;

    public BaivietDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }

    public void Save(Baiviet A) {
        try {
            session.getTransaction().begin();
            session.save(A);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void update(Baiviet A, int id) {
        try {
            session.getTransaction().begin();
            Baiviet baiviet = (Baiviet) session.get(Baiviet.class, id);
            //account.setLastname(A.getLastname());
            baiviet.setTieude(A.getTieude());
            baiviet.setNoidung(A.getNoidung());

            session.update(baiviet);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void delete(int id) {
        try {
            session.getTransaction().begin();
            session.delete(session.get(Baiviet.class, id));
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public List<Baiviet> getBVlikename(String name) {
        List<Baiviet> result = new ArrayList<Baiviet>();
        session.beginTransaction().begin();
        String sql = "from Baiviet where tieude like ?";
        try {
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List resultlist = query.list();
            session.getTransaction().commit();
            return resultlist;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }

        return result;

    }

    public List<Baiviet> getBVbyUser(int id) {
        List<Baiviet> result = new ArrayList<Baiviet>();
        session.beginTransaction().begin();
        String sql = "from Baiviet where ID_user =?";
        try {
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            List resultlist = query.list();
            session.getTransaction().commit();
            return resultlist;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }

        return result;
    }

    public List<Baiviet> getAllBaiviet() {
        List<Baiviet> result = new ArrayList<Baiviet>();
        session.beginTransaction().begin();
        String sql = "from Baiviet";
        try {
            Query query = session.createQuery(sql);

            List resultlist = query.list();
            session.getTransaction().commit();
            return resultlist;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }

        return result;
    }
    
}
